#!/bin/bash

set -e

if [[ -n "${RF_DEBUG}" ]]; then
    set -x
fi

DIR="$(dirname "$0")"

function usage() {
    echo -n \
        "Usage: $(basename "$0")

Publish container images to Elastic Container Registry (ECR) and
other artifacts to S3.
"
}

if [[ -n "${GIT_COMMIT}" ]]; then
    GIT_COMMIT="${GIT_COMMIT:0:7}"
else
    GIT_COMMIT="$(git rev-parse --short HEAD)"
fi

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
    if [ "${1:-}" = "--help" ]; then
        usage
    else
        if [[ -n "${AWS_ECR_ENDPOINT}" ]]; then
            echo "Building sbt container for migrations"
            GIT_COMMIT="${GIT_COMMIT}" docker-compose \
                -f "${DIR}/../docker-compose.test.yml" \
                build app-migrations

            echo "Building JARs (application, backsplash)"
            docker-compose \
                -f "${DIR}/../docker-compose.test.yml" \
                run --rm --no-deps \
                build "; api/assembly; backsplash-server/assembly"

            # Build publishable sbt projects as SNAPSHOT artifacts and
            # publish to Sonatype OSSRH (Snapshot Repository).
            if [ "$(whoami)" == "jenkins" ]; then
                echo "Publishing SNAPSHOT artifacts to Sonatype OSSRH (Snapshot Repository)"

                docker-compose \
                    -f "${DIR}/../docker-compose.test.yml" \
                    run --rm --no-deps --entrypoint "bash -c" \
                    -e SONATYPE_USERNAME="${SONATYPE_USERNAME}" \
                    -e SONATYPE_PASSWORD="${SONATYPE_PASSWORD}" \
                    -e GPG_KEY_ID="${GPG_KEY_ID}" \
                    build "
                        gpg --keyserver keyserver.ubuntu.com \
                            --recv-keys ${GPG_KEY_ID} &&
                        echo ${GPG_KEY} | base64 -d >signing_key.asc &&
                        gpg --import signing_key.asc &&
                        ./sbt publish
                    "
            fi

            echo "Building container images"
            GIT_COMMIT="${GIT_COMMIT}" docker-compose \
                -f "${DIR}/../docker-compose.yml" \
                -f "${DIR}/../docker-compose.test.yml" \
                build nginx-api api-server backsplash

            # Evaluate the return value of the get-login subcommand, which
            # is a docker login command with temporarily ECR credentials.
            eval "$(aws ecr get-login --no-include-email)"

            docker tag "raster-foundry-nginx-api:${GIT_COMMIT}" \
                "${AWS_ECR_ENDPOINT}/raster-foundry-nginx-api:${GIT_COMMIT}"
            docker tag "raster-foundry-api-server:${GIT_COMMIT}" \
                "${AWS_ECR_ENDPOINT}/raster-foundry-api-server:${GIT_COMMIT}"
            docker tag "raster-foundry-batch:${GIT_COMMIT}" \
                "${AWS_ECR_ENDPOINT}/raster-foundry-batch:${GIT_COMMIT}"
            docker tag "raster-foundry-app-migrations:${GIT_COMMIT}" \
                "${AWS_ECR_ENDPOINT}/raster-foundry-migrations:${GIT_COMMIT}"

            docker tag "raster-foundry-backsplash:${GIT_COMMIT}" \
                "${AWS_ECR_ENDPOINT}/raster-foundry-backsplash:${GIT_COMMIT}"

            docker push "${AWS_ECR_ENDPOINT}/raster-foundry-nginx-api:${GIT_COMMIT}"

            docker push "${AWS_ECR_ENDPOINT}/raster-foundry-api-server:${GIT_COMMIT}"
            docker push "${AWS_ECR_ENDPOINT}/raster-foundry-batch:${GIT_COMMIT}"
            docker push "${AWS_ECR_ENDPOINT}/raster-foundry-migrations:${GIT_COMMIT}"

            docker push "${AWS_ECR_ENDPOINT}/raster-foundry-backsplash:${GIT_COMMIT}"

        else
            echo "ERROR: No AWS_ECR_ENDPOINT variable defined."
            exit 1
        fi
    fi
fi
